package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.menggudf.entity.MedicalDB;
import com.menggudf.entity.NewCreatedMedicine;

@Repository
public class NewCreatedMedicineDao{
    
    private static final Log log = LogFactory.getLog(NewCreatedMedicineDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(NewCreatedMedicine transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(NewCreatedMedicine persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public NewCreatedMedicine merge(NewCreatedMedicine detachedInstance) {
        try {
            NewCreatedMedicine result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public NewCreatedMedicine findById(Long id) {
        try {
            NewCreatedMedicine instance = entityManager.find(NewCreatedMedicine.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public NewCreatedMedicine findByName(String name) {
        try {
            Query query = entityManager.createQuery("from NewCreatedMedicine m where m.medicineName=?1 ").setParameter(1, name);
            @SuppressWarnings("unchecked")
            List<NewCreatedMedicine> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception e) {
            log.error("find NewCreatedMedicine ByName fail",e);
            throw e;
        }
    }

    public List<NewCreatedMedicine> getNewCreatedMedicineList(String name, int pageSize, int offset) {
        try {
            StringBuffer sqld = new StringBuffer("from NewCreatedMedicine m ");
            if(!StringUtils.isEmpty(name)){
                sqld.append(" where m.medicineName like:name");
            }
            Query query = entityManager.createQuery(sqld.toString()).setFirstResult(offset).setMaxResults(pageSize);
            if(!StringUtils.isEmpty(name)){
                query.setParameter("name", "%"+name+"%");
            }
            return query.getResultList();
        } catch (Exception e) {
            log.error("getNewCreatedMedicineList fail",e);
            throw e;
        }
    }

    public long getNewCreatedMedicineListCount(String name) {
        try {
            StringBuffer sqld = new StringBuffer("select count(*) from NewCreatedMedicine m ");
            if(!StringUtils.isEmpty(name)){
                sqld.append(" where m.medicineName like:name");
            }
            Query query = entityManager.createQuery(sqld.toString());
            if(!StringUtils.isEmpty(name)){
                query.setParameter("name", "%"+name+"%");
            }
            return (long) query.getSingleResult();
        } catch (Exception e) {
            log.error("getNewCreatedMedicineListCount fail",e);
            throw e;
        }
    }

}
